package com.eis.persist.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.eis.function.out.rule.StatusRuleResult;
import com.eis.persist.model.function.PersistDataFetchSync;
import com.eis.persist.model.function.PersistFunction;
import com.eis.persist.model.function.PersistFunctionDestInvokeInfo;
import com.eis.persist.model.function.PersistFunctionSchema;
import com.eis.persist.model.function.PersistFunctionStatus;
import com.eis.persist.model.function.PersistFunctionStatusRule;

@Transactional
public interface FunctionDAO {
	/**
	 * Load all SAP functions with schema and parameter
	 * 
	 * @return
	 */
	public Map<Integer, PersistFunction> loadALlEnabledFunctions();
	
	public PersistFunction getFunctionById(String functionId);
	
	/**
	 * Check if the function schema exits and do create/update operation
	 * 
	 * @param persistFunction
	 * @param latestSchema
	 * @param fetchTime
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public void updateOrInsertFunctionSchema(PersistFunction persistFunction, ArrayList<HashMap<String, Object>> latestSchema, Date fetchTime);
	
	/**
	 * save function data to DB
	 * 
	 * @param persistFunction
	 * @param functionData
	 * @param date
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public int batchSaveFunctionData(PersistFunction persistFunction, ArrayList<HashMap<String, Object>> functionData, Date date) throws Throwable;
	
	/**
	 * Load one function schema by function ID
	 * 
	 * @param functionID
	 * @return
	 */
	public List<PersistFunctionSchema> getFunctionSchemaByFunctionId(String functionID);
	
	/**
	 * Load one function data by date
	 * 
	 * @param functionID
	 * @param date
	 * @return
	 */
	public List<Map<String, Object>> getFunctionData(String functionID, Date date);

	/**
	 * update the function data fetch time after success fetched from sap
	 * 
	 * @param date
	 */
	public void updateFunctionDateFetchTime(Date date, String functionId);
	
	public PersistFunctionDestInvokeInfo getFunctionDestInvokeInfo(String functionId);
	
	public int batchUpdateFunctionStatus(List<StatusRuleResult> ruleResult, String functionID);
	
    public PersistFunctionStatusRule loadFunctionStatusRule(String functionId);
    
    public List<PersistFunctionStatusRule> loadAllFunctionStatusRules();
	
	public boolean updateStatusRule(String functionId, String ruleXml, int maxRuleId);
	
	public List<PersistFunctionStatus> loadFunctionStatuses();
	
	public PersistDataFetchSync getPersistDataFetchSync(String currentRunnerId, int runduration, int faultUpdateTimeout);
}
